package com.controller;

import com.entity.WeiJianProjectInfo;
import com.service.WeiJianProjectInfoService;
import com.dto.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/api/weijian/project")
public class WeiJianProjectInfoController {

    @Autowired
    private WeiJianProjectInfoService weiJianProjectInfoService;

    @PostMapping
    public WeiJianProjectInfo create(@RequestBody WeiJianProjectInfo weiJianProjectInfo) {
        return weiJianProjectInfoService.save(weiJianProjectInfo);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable(value = "id") Integer id) {
        weiJianProjectInfoService.deleteById(id);
    }

    @PutMapping("/{id}")
    public WeiJianProjectInfo update(@PathVariable(value = "id") Integer id, @RequestBody WeiJianProjectInfo weiJianProjectInfo) {
        weiJianProjectInfo.setId(id);
        return weiJianProjectInfoService.save(weiJianProjectInfo);
    }

    @GetMapping("/{id}")
    public WeiJianProjectInfo detail(@PathVariable(value = "id") Integer id) {
        return weiJianProjectInfoService.findById(id).orElse(null);
    }

    @GetMapping("/page")
    public Page<WeiJianProjectInfo> page(@RequestParam(defaultValue = "1") int pageNum,
                       @RequestParam(defaultValue = "10") int pageSize,
                       @RequestParam(required = false) String projectName) {
        Page<WeiJianProjectInfo> page;
        if (projectName != null && !projectName.isEmpty()) {
            page = weiJianProjectInfoService.findByProjectNameContaining(projectName, pageNum, pageSize);
        } else {
            page = weiJianProjectInfoService.findAll(pageNum, pageSize);
        }
        return page;
    }
}